package com.sky.controller.user;

import com.sky.pojo.AddressBook;
import com.sky.result.Result;
import com.sky.service.user.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 查询当前登录用户的所有地址信息
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> queryAll(){
        return Result.success(addressBookService.queryAll());
    }

    /**
     * 查询默认地址
     */
    @GetMapping("/default")
    public Result<AddressBook> queryDefault(){
        return Result.success(addressBookService.queryDefault());
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/default")
    public Result<String> setDefault(@RequestBody AddressBook addressBook){
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    public Result<AddressBook> queryById(@PathVariable Long id){
        return Result.success(addressBookService.queryById(id));
    }

    /**
     * 根据id删除地址
     */
    @DeleteMapping
    public Result<String> delete(Long id){
        addressBookService.delete(id);
        return Result.success();
    }

    /**
     * 新增地址
     */
    @PostMapping
    public Result add(@RequestBody AddressBook addressBook){
        addressBookService.add(addressBook);
        return Result.success();
    }

    /**
     * 根据id修改地址
     */
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        addressBookService.update(addressBook);
        return Result.success();
    }





}
